<?php
/**
 * [KeCms] Copyright (c) 2017 kecms.cn
 * [Author] King east ( 1207877378@qq.com )
 */

namespace app\admin\controller\auth;



use app\admin\model\AdminLog;
use app\admin\model\AdminRole;
use app\admin\model\AdminRule;
use app\admin\model\AdminRuleAccess;
use ke\Controller;
use think\Cache;
use think\Validate;

class GroupController extends Controller
{
    public function lists()
    {
        $type=input('get.type');
        switch ($type){
            case 1:
                $list=AdminRole::with(['parent','ruleList'])->select()
                    ->toArray();

                $rule=AdminRule::field('id,parent_id,title')->order('showorder','asc')->select()->toArray();

                foreach ($list as &$g){
                    $g['rule_list']=array_column($g['rule_list'],'rule_id');
                }

                $this->result([
                    'list'=>list_to_tree($list,'id','parent_id'),
                    'rule'=>list_to_tree($rule,'id','parent_id')
                ]);
            default:
                return $this->fetch();
        }
    }

    /**
     * 创建或保存内容
     * @param bool $data
     */
    public function save()
    {
        $form=getForm();
        $vali=new Validate([
            'name|用户组名称'=>'require|max:20',
            'content|备注内容'=>'max:255'
        ]);
        if(!$vali->check($form)){
            $this->error($vali->getError());
        }
        if(empty($form['id'])){
            $data=new AdminRole;
        }else{
            $data=AdminRole::where('id',$form['id'])->find();
            if(!$data){
                $this->error("管理组“{$form['id']}”不存在");
            }
        }
        if(AdminRole::where(function($rs) use($form,$data){
            $rs->where('name',$form['name']);
            if(!empty($form['id'])){
                $rs->where('name','<>',$data->name);
            }
        })->value('id')){
            $this->error("用户组名称“{$form['name']}”已经存在");
        }
        $data->name=$form['name'];
        $data->parent_id=empty($form['parent_id']) ? 0 : $form['parent_id'];
        $data->save();
        AdminRuleAccess::where('role_id',$data->id)->delete();
        Cache::rm('adminMenuList/'.$data->id);
        Cache::rm('adminRuleAccess/'.$data->id);
        if(isset($form['rule_list']) && is_array($form['rule_list'])){
            $rule=new AdminRuleAccess();
            $list=[];
            foreach ($form['rule_list'] as $g){
                $list[]=['rule_id'=>$g,'role_id'=>$data->id];
            }
            $rule->saveAll($list);

        }
        AdminLog::write('设置管理组ID：'.$data->id);
        $this->result($data->toArray());

    }

    /**
     * 删除
     */
    public function delete()
    {
        $id=input('get.id');
        if(empty($id)){
            $this->error('管理组不存在','lists');
        }
        $data=AdminRole::where('id',$id)->find();
        if(!$data){
            $this->error('管理组不存在');
        }
        if(AdminRole::where('parent_id',$data->id)->find()){
            $this->error('管理组不为空，删除失败。');
        }
        $data->delete();
        AdminLog::write('删除分组ID：'.$id);
        $this->result(['id'=>$id]);


    }
}